package com.istudy.service.imp;

import com.istudy.dao.BaseDao;
import com.istudy.dao.interfaces.CommentDao;
import com.istudy.dao.impl.CommentDaoImpl;
import com.istudy.dao.xyjdao.impl.CommentimplImpl;
import com.istudy.entity.Comment;
import com.istudy.entity.User;
import com.istudy.entity.Video;
import com.istudy.service.interfaces.CommentService;
import com.istudy.utils.JDBCUtils;
import com.istudy.utils.Page;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xdd
 * @create 2020-08-24 14:56
 */
public class CommentServiceImpl implements CommentService {

    CommentimplImpl commentimpl=new CommentimplImpl(JDBCUtils.getConnection());//业务逻辑层对象

    /**
     * 获取用户对应的品论
     *
     * @return 评论类集合
     */
    @Override
    public List<Comment> selectCommentinformation() {
        List<Comment> comments=null;
        try {
            comments= commentimpl.selectCommentinformation(0,5);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comments;
    }
    /**
     * 新增评论信息
     *
     * @param comment 参数数组
     * @return 是否成功
     */
    @Override
    public boolean insertComment(Comment comment) {
        Connection connection = null;
        int count = 0;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            CommentDao CommentDao = new CommentDaoImpl();
            count = CommentDao.insertComment(comment);
            if(count > 0){
                flag = true;
            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
    /**
     * 获取用户对应的评论
     *
     * @return 评论类集合
     */
    @Override
    public List<Comment> getCommentByUser(HttpServletRequest request, int num) {
        Connection connection = null;
        Page page=new Page();
        List<Comment> list = new ArrayList<>();
        User user=(User)request.getSession().getAttribute("user");
        try {
            connection = BaseDao.getConnection();
            CommentDao commentDao = new CommentDaoImpl();
            page.setCount(commentDao.getCount(user));
            if (num!=0){
                page.setPageNo(num);
            }
            list = commentDao.queryCommentByUser(user,page);
            request.setAttribute("commentList",list);
            request.setAttribute("page",page);
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
    /**
     * 获取视频对应的评论
     *
     * @return 评论类集合
     */
    @Override
    public List<Comment> getCommentByVideo(HttpServletRequest request,int num) {
        Connection connection = null;
        List<Comment> list = new ArrayList<>();
        Page page=new Page();
        Video video=(Video)request.getAttribute("video");
        try {
            connection = BaseDao.getConnection();
            CommentDao commentDao = new CommentDaoImpl();
            page.setCount(commentDao.getCount(video));
            if (num!=0){
                page.setPageNo(num);
            }
            list = commentDao.queryCommentByVideo(video,page);
            request.setAttribute("commentList",list);
            request.setAttribute("page",page);
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 删除评论记录
     *
     * @param request
     * @return
     */
    @Override
    public boolean delComment(HttpServletRequest request) {
        Connection connection = null;
        int i=-1;
        boolean b=false;
        int id=Integer.parseInt(request.getParameter("id"));
        try {
            connection = BaseDao.getConnection();
            CommentDao commentDao=new CommentDaoImpl();
            i=commentDao.deleteComment(id);
            connection.close();
            if(i>0){
                b=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }
}
